# 设置工作目录
setwd("F:/第6章/01-任务程序")

# ----------------------分析用户信息完善程度与逾期率关系------------------------
df.tr.master <- read.csv("./data/Training_Master.csv")  # 读取训练集
df.ts.master <- read.csv("./data/Test_Master.csv")  # 读取测试集

df.ts.master["target"] <- NA  # 测试集没有target，添加一列，并用NA赋值，方便后续合表
df.tr.master[df.tr.master == "不详"] <- NA
df.ts.master[df.ts.master == "不详"] <- NA
df.master <- rbind(df.tr.master, df.ts.master)

df.master[, "na.num"] <- apply(is.na(df.master), 1, sum)  # 缺失值个数

# 绘制用户信息完整度和逾期率的关系图
# 绘制主表中用户信息缺失的情况，以缺失个数为纵坐标
plot(df.master[order(df.master[, "na.num"]), "na.num"],
     ylab = "用户缺失信息的个数")
lines(x = c(0:50000), y = rep(2, 50001), type = "l", col = "red", lwd = 2)
lines(x = c(0:50000), y = rep(10, 50001), type = "l", col = "red", lwd = 2)
# 剔除离群点，离群点样本数较少，存在偶然性
rid.out <- which((df.master[, "na.num"] <= 10 & df.master[, "na.num"] >= 2))
rid.out.tg <- df.master[rid.out, "target"]
na.num.fre <- table(rid.out.tg, df.master[rid.out, "na.num"])
tg.fre <- na.num.fre[2,] / (na.num.fre[1,] + na.num.fre[2,])  # 计算逾期率
row.names(na.num.fre)
barplot(tg.fre)
group <- c()
for (i in 1:7) {
  if (i %% 3 == 1) {
    tg.fre.group <- tg.fre[i] + tg.fre[i + 1] + tg.fre[i + 2]
    group <- c(group, tg.fre.group)
  }
}
# 分组
barplot(group, xaxt = "n", ylab = "逾期率", 
        xlab = "用户信息缺失的个数", ylim = c(0, 0.25))
text.group <- c("2-4", "5-7", "8-10")
axis(1, at = c(0.7, 1.9, 3.1), labels = text.group, tick = FALSE)



# ----------------------分析用户信息修改情况与逾期率关系------------------------
# 分布分析
df.tr.update <- read.csv("./data/Training_Userupdate.csv")  # 读取训练集
df.ts.update <- read.csv("./data/Test_Userupdate.csv")  # 读取测试集
df.update <- rbind(df.tr.update, df.ts.update)  # 合并update数据

# 计算用户更新信息的天数
df.update.num <- table(unique(df.update[c("Idx", "UserupdateInfo2")])$Idx)
df.update.num <- data.frame(df.update.num)
colnames(df.update.num) <- c("Idx", "update.num")

# 绘制用户修改信息天数与逾期率的关系图
# 绘制更新信息表中用户修改信息的情况，以修改的天数为纵坐标
plot(df.update.num[order(df.update.num[, "update.num"]), "update.num"],
     ylab = "用户修改信息的天数")
lines(x = c(0:50000), y = rep(5, 50001), type = "l", col = "red", lwd = 2)
rid.out <- which(df.update.num[, "update.num"] <= 5)  # 剔除离群点
update.num <- merge(df.master, df.update.num, by = "Idx")
rid.out.tg <- update.num[rid.out, "target"]
update.num <- table(rid.out.tg, df.update.num[rid.out, "update.num"])
update.num <- update.num[2, ] / (update.num[1,] + update.num[2, ])  # 计算逾期率
barplot(update.num, ylim = c(0, 0.12), xlab = "修改信息的天数", ylab = "逾期率")



# -------------------分析用户所在区域经济发展状况与逾期率关系-------------------
# 省GDP
df.gdp.prov <- read.csv("./data/Province_GDP.csv")
library(stringr)
# 去除省或市后面的空格
df.gdp.prov[, "province"] <- str_replace_all(df.gdp.prov[, "province"], " ", "")
# 将省字去掉
df.gdp.prov[, "province"] <- str_replace_all(df.gdp.prov[, "province"], "省", "")
# 将市字去掉
df.gdp.prov[, "province"] <- str_replace_all(df.gdp.prov[, "province"], "市", "")
df.gdp.prov <- df.gdp.prov[, c("province", "provGDPpp")]
prov <- c("UserInfo_7", "UserInfo_19")
# 去掉省字
for (i in (1:length(prov))) {
  df.master[, prov[i]] <- str_replace_all(df.master[, prov[i]], "省", "")
}
# 将省人均GDP加入主表中
df.master <- merge(df.master, df.gdp.prov, by.x = "UserInfo_7", 
                   by.y = "province", all = TRUE, sort = FALSE)

# 绘制各省逾期情况图
gdp.tg <- data.frame(df.master$target, df.master$UserInfo_7, 
                     df.master$provGDPpp)
gdp.tg <- gdp.tg[order(gdp.tg[, 3], decreasing = FALSE),]
colnames(gdp.tg) <- c("target", "province", "provGDPpp")
gdp.fre <- table(gdp.tg$target, gdp.tg$provGDPpp)
gdp.fre <- gdp.fre[2,] / (gdp.fre[1,] + gdp.fre[2,])
barplot(gdp.fre, xaxt = "n", ylim = c(0, 0.12), ylab = "逾期率")
text.x = c("甘肃", "贵州", "云南", "西藏", "广西", "安徽", "江西", "山西",
           "四川", "河南", "海南", "黑龙江", "青海", "河北", "湖南",
           "新疆", "宁夏", "陕西", "湖北", "重庆", "吉林", "山东", "福建",
           "广东", "辽宁", "内蒙古", "浙江", "江苏", "上海", "北京", "天津")
num.x = seq(0.8, 36.963, 1.193)
axis(1, at = num.x, labels = text.x, las = 2, tick = FALSE)
par(new = T)
gdp.order <- df.gdp.prov[order(df.gdp.prov[, 2]),]
plot(gdp.order[, 2], ann = FALSE, type = "l", lwd = 2, col = "red", 
     axes = FALSE, sub = "省人均GDP")
gdp = seq(0, 120000, 30000)
axis(4, at = gdp, labels = gdp, col = "red", lwd = 2)
legend(2, 105000, lty = c(1, NA), pch = c(NA, 15), lwd = c(2, 1),
       col = c("red", "gray"), legend = c("省人均GDP", "各省逾期率"))



# -------------------------分析用户借款月份与逾期率关系-------------------------
# 获取借款成交的月份
df.master[, "listing.month"] <- as.numeric(format(as.Date(df.master$ListingInfo, 
                                                        format = "%Y/%m/%d"), "%m"))
df.master$ListingInfo <- NULL

write.csv(df.master, "./tmp/df_master.csv", row.names = FALSE)  # 写出数据

# 绘制用户借款月份和逾期率的关系图
mon.fre <- table(df.master$target, df.master$listing.month)
mon.fre <- mon.fre[2, ] / (mon.fre[1, ] + mon.fre[2, ])
barplot(mon.fre, xlab = "用户借款月份", ylab = "逾期率")
